var prefStr;

function pow_load_prefs() {
	try {
		powBundle = document.getElementById("bundle_pow");
		document.getElementById("pow_pass_protected_checkbox").checked = pow_bc_pass_protected();
		document.getElementById("pow_loopback_only").checked = pow_bc_get_loopback_only();
		document.getElementById("port").value = pow_bc_get_port_number();
		document.getElementById("loginID").value = pow_bc_get_login();
		document.getElementById("pass").value = pow_bc_get_password();
  	document.getElementById("loginID").disabled = !pow_bc_pass_protected();
  	document.getElementById("pass").disabled = !pow_bc_pass_protected();
    document.getElementById("root_dir").value = get_pow_root_dir();
		pow_pop_mimelist();
	} catch (e) {
		log_error(e);
	}
}

function pow_do_ok() {
	try {
    var prefStr = pow_sitelist_to_string();
    pow_set_mimelist_pref(prefStr); 
    pow_bc_set_port_pref(document.getElementById("port").value);
    pow_set_pass_protected(document.getElementById("pow_pass_protected_checkbox").checked);
    pow_set_loopback_only(document.getElementById("pow_loopback_only").checked);
    pow_set_login(document.getElementById("loginID").value);
    pow_set_password(document.getElementById("pass").value);
    pow_set_root_dir(document.getElementById("root_dir").value);
    window.close();
	} catch (e) {
		log_error(e);
	}
}

function pow_add_mime() {
		try {
    var textbox = document.getElementById("extTextBox");
    var siteName = textbox.value;
    var mimebox = document.getElementById("mimeEntry");
    var mimeName = mimebox.value;
    var binaryBox = document.getElementById("binaryCheck");
    var isBinary = binaryBox.checked;

    if(siteName.length == 0)
        return false;

    var mimeList = document.getElementById("mime_types");
    if(! pow_list_contains_site(siteName)) {
				var item=mimeList.appendItem(siteName);
				item.appendChild( document.createElement('listcell') ).setAttribute('label', siteName);
				item.appendChild( document.createElement('listcell') ).setAttribute('label', mimeName);
				item.appendChild( document.createElement('listcell') ).setAttribute('label', isBinary);
        mimeList.ensureElementIsVisible(item);
		} else {
			// change value
		}
    textbox.value = "";
    mimebox.value = "";
    pow_site_input(textbox);
    textbox.focus();
		prefStr = pow_sitelist_to_string();
		} catch (e) {
			// noop 
		}
    return true;
}

function pow_chooseRoot() {
	var nsIFilePicker = Components.interfaces.nsIFilePicker;
	var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
	fp.init(window, "Select a Root Directory", nsIFilePicker.modeGetFolder);
	var res = fp.show();
	if (res == nsIFilePicker.returnOK){
		var my_root_dir = fp.file;
		var root_dir_field = document.getElementById("root_dir");
		root_dir_field.value = my_root_dir.path;
	}
	
}

function pow_pop_mimelist() {
	try {
	var prefStr = pow_get_mimelist_pref();
	var i;
	var mimeList = document.getElementById("mime_types");
	if(prefStr) {
		var array = prefStr.split(",");
		for (i = 0; i < array.length; i+=3) {
				// PUT BACK
				var item=mimeList.appendItem(array[i]);
				item.appendChild( document.createElement('listcell') ).setAttribute('label', array[i]);
				item.appendChild( document.createElement('listcell') ).setAttribute('label', array[i+1]);
				item.appendChild( document.createElement('listcell') ).setAttribute('label', array[i+2]);
		}
	}
	mimeList.setAttribute('rows',3)
	} catch(e) {
		log_error(e);
	}
}

function pow_sitelist_to_string() {
    var siteList = document.getElementById("mime_types");
    var numRows = siteList.getRowCount();
    var prefStr = "";
    var i;
    for(i = 0; i < numRows; i++ ) {
        if(i != 0) prefStr += ",";
				prefStr += siteList.getItemAtIndex(i).getAttribute('label') + ",";
				prefStr += siteList.getItemAtIndex(i).childNodes.item(1).getAttribute('label') + ",";
				prefStr += siteList.getItemAtIndex(i).childNodes.item(2).getAttribute('label');
    }
    return prefStr;
}


function pow_site_input(siteField) {
  document.getElementById("btnAdd").disabled = !siteField.value;
}

function pow_pass_protected_input(siteField) {
	try {
  	document.getElementById("loginID").disabled = !siteField.checked;
  	document.getElementById("pass").disabled = !siteField.checked;
	} catch(e) {
		log_error(e);
	}
}

function pow_site_selected(siteList) {
  document.getElementById("btnRemove").disabled = (siteList.selectedIndex == -1);
}

function pow_list_contains_site(site) {
    var siteList = document.getElementById("mime_types");
    var numRows = siteList.getRowCount();
    var i;
    for(i = 0; i < numRows; i++) {
        if(siteList.getItemAtIndex(i).label == site)
            return true;
    }
    return false;
}

function pow_remove_site() {
    var siteList = document.getElementById("mime_types");
    var index = siteList.selectedIndex;
    if(index != -1) {
        siteList.removeItemAt(index);
        document.getElementById("btnRemove").disabled = true;
    } 
}
